package com.hawk.eye.framework.config;

import com.hawk.eye.api.storage.ILogMessageStorage;
import com.hawk.eye.api.webhook.IWebhookRequester;
import com.hawk.eye.framework.cache.redis.RedisCache;
import com.hawk.eye.framework.plugins.PluginBeanRegister;
import com.hawk.eye.framework.utils.ExceptionUtils;
import com.hawk.eye.framework.utils.spring.SpringUtils;
import com.hawk.eye.framework.cache.ICache;
import com.hawk.eye.framework.cache.local.LocalCache;
import com.hawk.eye.framework.config.properties.HawkEyeServerProperties;
import com.hawk.eye.framework.plugins.webhook.ExternalJarLoader;
import com.hawk.eye.framework.plugins.webhook.WebhookFactory;
 import com.hawk.eye.server.delegate.LogMessageStorageDelegate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @className: HawkEyeServerConfiguration
 * @description:
 * @author: salad
 * @date: 2022/10/9
 **/
@Configuration
@EnableConfigurationProperties(HawkEyeServerProperties.class)
public class HawkEyeServerConfiguration {

    @Bean
    @SuppressWarnings(value = {"rawtypes"})
    public ICache cache(HawkEyeServerProperties properties, RedisTemplate redisTemplate) {
        if (HawkEyeServerProperties.CacheType.LOCAL.equals(properties.getCacheType())){
            return new LocalCache();
        }
        return new RedisCache(redisTemplate);
    }

//    @Bean("authFilterRegistration")
//    public FilterRegistrationBean<AuthFilter> authFilterRegistration(ICache cache) {
//        FilterRegistrationBean<AuthFilter> registration = new FilterRegistrationBean<>();
//        registration.setDispatcherTypes(DispatcherType.REQUEST);
//        registration.setFilter(new AuthFilter(cache));
//        registration.addUrlPatterns("/hawk-eye/service/push-alarm-log");
//        registration.setName("authFilter");
//        registration.setOrder(Ordered.LOWEST_PRECEDENCE);
//        return registration;
//    }


    public void loadPluginJar(HawkEyeServerProperties properties) {
        List<IWebhookRequester> webhookRequester = new ExternalJarLoader()
                .loadJar(properties.getPluginWebhookPath(), IWebhookRequester.class);
        System.out.println("webhookRequester:"+webhookRequester);
        PluginBeanRegister.regist(webhookRequester);
    }

    @Bean("webhookFactory")
    public WebhookFactory webhookFactory(HawkEyeServerProperties properties) {
        this.loadPluginJar(properties);
        WebhookFactory webhookFactory = new WebhookFactory();
        Map<String, IWebhookRequester> requesterMap = SpringUtils.getBeansOfType(IWebhookRequester.class);
        Collection<IWebhookRequester> values = requesterMap.values();
        values.forEach(webhookFactory::add);
        return webhookFactory;
    }

    @Bean
    @ConditionalOnMissingBean
    public LogMessageStorageDelegate alarmLogStorageDelegate(HawkEyeServerProperties properties) {
        List<ILogMessageStorage> alarmLogStorages = new ExternalJarLoader()
                .loadJar(properties.getPluginStoragePath(), ILogMessageStorage.class);
        PluginBeanRegister.regist(alarmLogStorages);
        LogMessageStorageDelegate storageDelegate = new LogMessageStorageDelegate();
        Map<String, ILogMessageStorage> requesterMap = SpringUtils.getBeansOfType(ILogMessageStorage.class);
        Collection<ILogMessageStorage> values = requesterMap.values();
        values.forEach(storageDelegate::add);
        String storageType = properties.getStorageType();
        ExceptionUtils.isEmptyThrow(storageType, "请确认配置文件是否配置了storage-type属性，并且是否为:[{}]或者[{}]");
        storageDelegate.setDefaultStorage(storageDelegate.getAlarmLogStorage(storageType));
        return storageDelegate;
    }

}
